package com.ruoyi.common.utils;

import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.Base64;

@Slf4j
public class FileUtils {

    /**
     * 创建生产语音的文本文件
     * @param prefix
     * @param filePath
     * @param fileName
     * @param txtContent
     * @return
     */
    public static String  writeSayTxtFile(String prefix, String filePath, String fileName, String txtContent){
        try {
            //保存文件
            log.info("fileName:{}",prefix + "/" +  filePath + "/" + fileName);
            File file = new File(prefix + "/" + filePath);
            if(!file.exists()){
                log.info("创建文件夹：{}",prefix + "/" +  filePath);
                file.mkdirs();
            }
            File fileF = new File(prefix + "/" +  filePath + "/" + fileName);
            if(!fileF.exists()){
                log.info("创建文件：{}",prefix + "/" +  filePath + "/" + fileName);
                fileF.createNewFile();
            }

            // 创建FileOutputStream对象，用于写入文件
            try (FileOutputStream fileOutputStream = new FileOutputStream(fileF)) {
                fileOutputStream.write(txtContent.getBytes());
            } catch (IOException e) {
                System.err.println("写入文件时发生错误: " + e.getMessage());
            }

        } catch (FileNotFoundException e) {
            log.error("文件异常：",e);
            e.printStackTrace();
        } catch (IOException ie){
            log.error("IO异常：",ie);
            ie.printStackTrace();
        }
        log.info("文本目标文件：{}",filePath+"/"+fileName);
        return filePath+"/"+fileName;
    }

    /**
     * 写入指令命令
     * @param filePath
     * @param content
     */
    public static void writeFile(String filePath,String content){
        BufferedWriter writer = null;
        try {
            // 创建FileWriter对象，第二个参数为true表示以追加模式写入
            FileWriter fileWriter = new FileWriter(filePath, true);
            writer = new BufferedWriter(fileWriter);

            // 写入新的一行文本
            writer.newLine(); // 换行
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (writer != null) {
                    writer.close(); // 关闭BufferedWriter
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        writeSayTxtFile("/Users/yangxingze/Desktop/project/2024/aivideo","/ai-media/doc","/123.txt","abcde");
    }
}
